package cn.k.controller;


import cn.k.domain.Business;
import cn.k.qo.ConsumptionReportQueryObject;
import cn.k.service.*;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumptionReport")
@Transactional
public class ConsumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {
        PageInfo<Map<String, Object>> pageInfo = consumptionReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询门店
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);
        return "/consumptionReport/list";
    }


    @RequestMapping("/listCharts")
    public String listCharts(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {

        qo.setPageSize(Integer.MAX_VALUE);  //设置每页可以容纳20多亿条数据
        PageInfo<Map<String, Object>> mapList = consumptionReportService.query(qo);
        //x轴--->groupType
        ArrayList<Object> groupTypeList = new ArrayList<>();
        //总消费
        ArrayList<Object> totalAmountList = new ArrayList<>();
        //总实收
        ArrayList<Object> payAmountList = new ArrayList<>();
        //总优惠
        ArrayList<Object> discountAmountList = new ArrayList<>();
        //订单数量
        ArrayList<Object> countList = new ArrayList<>();
        //遍历
        for (Map<String, Object> map : mapList.getList()) {
            groupTypeList.add(map.get("groupType"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountAmountList.add(map.get("discountAmount"));
            countList.add(map.get("count"));
        }

        //装进model
        //数据转成JSON格式,chars图表中的数据JSON格式放进去比较合适
        model.addAttribute("groupType", JSON.toJSONString(groupTypeList));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmount", JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmount", JSON.toJSONString(discountAmountList));
        model.addAttribute("count", JSON.toJSONString(countList));

        return "/consumptionReport/listCharts";
    }


}
